/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.sesame.domain.Album;
import com.sesame.domain.Answer;
import com.sesame.domain.Ask;
import com.sesame.domain.Comment;
import com.sesame.domain.Guestbook;
import com.sesame.domain.Image;
import com.sesame.domain.User;
import com.sesame.service.AlbumService;
import com.sesame.service.ContactService;
import com.sesame.service.UserService;
import com.sesame.toolkit.Encrypt;

/**
 * @author voidman
 * 
 */
public class ImageDetailController extends AbstractController {

    private AlbumService albumService;
    private ContactService contactService;
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setAlbumService(AlbumService albumService) {
        this.albumService = albumService;
    }

    public void setContactService(ContactService contactService) {
        this.contactService = contactService;
    }

    @SuppressWarnings("unchecked")
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        Integer userId = (Integer) request.getSession().getAttribute("userId");
        String nickName = (String) request.getSession()
                .getAttribute("nickName");
        String headPic = (String) request.getSession().getAttribute("headPic");
        String content = request.getParameter("content");
        Integer imageId = Integer.parseInt(request.getParameter("imageId"));
        Image image = albumService.showImage(imageId);
        Integer user2Id = image.getUserId();
        User user = userService.showUser(user2Id);
        if (userId != null) {

            if (userId.intValue() != user2Id.intValue()) {
                boolean isFriend = contactService.isFriend(user2Id, userId);
                if (image.getPrivacy().equals("2")
                        || (!isFriend && image.getPrivacy().equals("1"))) {
                    return new ModelAndView("redirect:error.html");
                }
            }
        } else {
            if (image.getPrivacy().equals("2")
                    || (image.getPrivacy().equals("1"))) {
                return new ModelAndView("redirect:error.html");
            }
        }

        if (content != null && !content.equals("")) {
            if (nickName == null) {
                nickName = request.getParameter("nickName");
                headPic = Encrypt.MapEncrypt("00000/000");
                userId = 0;
            }
            if (nickName != null && !nickName.equals(""))
                albumService.newComment(imageId, userId, nickName, content,
                        headPic);
        }
        Map model = new HashMap();

        List<Comment> commentList = albumService.listComment(imageId);
        model.put("image", image);
        model.put("user", user);
        model.put("commentList", commentList);

        return new ModelAndView("imageDetail", model);

    }

}